﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using IPC.Communication.Framework;
using IPC.Communication.Framework.Core;
using IPC.Communication.Framework.Core.IMessage;
using IPC.Communication.Framework.Core.Net;

namespace IPC.Communication.Framework.Enthernet
{
    public class NetSimplifyClient : NetworkDoubleBase
    {
	    public NetSimplifyClient(string ipAddress, int port)
	    {
		    base.ByteTransform = new RegularByteTransform();
		    IpAddress = ipAddress;
		    Port = port;
	    }

	    public NetSimplifyClient(IPAddress ipAddress, int port)
	    {
		    base.ByteTransform = new RegularByteTransform();
		    IpAddress = ipAddress.ToString();
		    Port = port;
	    }

	    public NetSimplifyClient()
	    {
		    base.ByteTransform = new RegularByteTransform();
	    }

	    protected override INetMessage GetNewNetMessage()
	    {
		    return new HslMessage();
	    }

	    protected override OperateResult InitializationOnConnect(Socket socket)
	    {
		    if (isUseAccountCertificate)
		    {
			    return AccountCertificate(socket);
		    }
		    return OperateResult.CreateSuccessResult();
	    }

        //[AsyncStateMachine(typeof(<InitializationOnConnectAsync>d__5))]
        //[DebuggerStepThrough]
        //protected override Task<OperateResult> InitializationOnConnectAsync(Socket socket)
        //{
        //    <InitializationOnConnectAsync>d__5 stateMachine = new <InitializationOnConnectAsync>d__5();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.socket = socket;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

	    public OperateResult<string> ReadFromServer(NetHandle customer, string send)
	    {
		    OperateResult<byte[]> operateResult = ReadFromServerBase(HslProtocol.CommandBytes(customer, base.Token, send));
		    if (!operateResult.IsSuccess)
		    {
			    return OperateResult.CreateFailedResult<string>(operateResult);
		    }
		    return OperateResult.CreateSuccessResult(Encoding.Unicode.GetString(operateResult.Content));
	    }

	    public OperateResult<string[]> ReadFromServer(NetHandle customer, string[] send)
	    {
		    OperateResult<byte[]> operateResult = ReadFromServerBase(HslProtocol.CommandBytes(customer, base.Token, send));
		    if (!operateResult.IsSuccess)
		    {
			    return OperateResult.CreateFailedResult<string[]>(operateResult);
		    }
		    return OperateResult.CreateSuccessResult(HslProtocol.UnPackStringArrayFromByte(operateResult.Content));
	    }

	    public OperateResult<byte[]> ReadFromServer(NetHandle customer, byte[] send)
	    {
		    return ReadFromServerBase(HslProtocol.CommandBytes(customer, base.Token, send));
	    }

	    public OperateResult<NetHandle, string> ReadCustomerFromServer(NetHandle customer, string send)
	    {
		    OperateResult<NetHandle, byte[]> operateResult = ReadCustomerFromServerBase(HslProtocol.CommandBytes(customer, base.Token, send));
		    if (!operateResult.IsSuccess)
		    {
			    return OperateResult.CreateFailedResult<NetHandle, string>(operateResult);
		    }
		    return OperateResult.CreateSuccessResult(operateResult.Content1, Encoding.Unicode.GetString(operateResult.Content2));
	    }

	    public OperateResult<NetHandle, string[]> ReadCustomerFromServer(NetHandle customer, string[] send)
	    {
		    OperateResult<NetHandle, byte[]> operateResult = ReadCustomerFromServerBase(HslProtocol.CommandBytes(customer, base.Token, send));
		    if (!operateResult.IsSuccess)
		    {
			    return OperateResult.CreateFailedResult<NetHandle, string[]>(operateResult);
		    }
		    return OperateResult.CreateSuccessResult(operateResult.Content1, HslProtocol.UnPackStringArrayFromByte(operateResult.Content2));
	    }

	    public OperateResult<NetHandle, byte[]> ReadCustomerFromServer(NetHandle customer, byte[] send)
	    {
		    return ReadCustomerFromServerBase(HslProtocol.CommandBytes(customer, base.Token, send));
	    }

	    private OperateResult<byte[]> ReadFromServerBase(byte[] send)
	    {
		    OperateResult<NetHandle, byte[]> operateResult = ReadCustomerFromServerBase(send);
		    if (!operateResult.IsSuccess)
		    {
			    return OperateResult.CreateFailedResult<byte[]>(operateResult);
		    }
		    return OperateResult.CreateSuccessResult(operateResult.Content2);
	    }

	    private OperateResult<NetHandle, byte[]> ReadCustomerFromServerBase(byte[] send)
	    {
		    OperateResult<byte[]> operateResult = ReadFromCoreServer(send);
		    if (!operateResult.IsSuccess)
		    {
			    return OperateResult.CreateFailedResult<NetHandle, byte[]>(operateResult);
		    }
		    return HslProtocol.ExtractHslData(operateResult.Content);
	    }

        //[AsyncStateMachine(typeof(<ReadFromServerAsync>d__14))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<string>> ReadFromServerAsync(NetHandle customer, string send)
        //{
        //    <ReadFromServerAsync>d__14 stateMachine = new <ReadFromServerAsync>d__14();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.customer = customer;
        //    stateMachine.send = send;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadFromServerAsync>d__15))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<string[]>> ReadFromServerAsync(NetHandle customer, string[] send)
        //{
        //    <ReadFromServerAsync>d__15 stateMachine = new <ReadFromServerAsync>d__15();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.customer = customer;
        //    stateMachine.send = send;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadFromServerAsync>d__16))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<byte[]>> ReadFromServerAsync(NetHandle customer, byte[] send)
        //{
        //    <ReadFromServerAsync>d__16 stateMachine = new <ReadFromServerAsync>d__16();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.customer = customer;
        //    stateMachine.send = send;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadCustomerFromServerAsync>d__17))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<NetHandle, string>> ReadCustomerFromServerAsync(NetHandle customer, string send)
        //{
        //    <ReadCustomerFromServerAsync>d__17 stateMachine = new <ReadCustomerFromServerAsync>d__17();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<NetHandle, string>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.customer = customer;
        //    stateMachine.send = send;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadCustomerFromServerAsync>d__18))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<NetHandle, string[]>> ReadCustomerFromServerAsync(NetHandle customer, string[] send)
        //{
        //    <ReadCustomerFromServerAsync>d__18 stateMachine = new <ReadCustomerFromServerAsync>d__18();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<NetHandle, string[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.customer = customer;
        //    stateMachine.send = send;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadCustomerFromServerAsync>d__19))]
        //[DebuggerStepThrough]
        //public Task<OperateResult<NetHandle, byte[]>> ReadCustomerFromServerAsync(NetHandle customer, byte[] send)
        //{
        //    <ReadCustomerFromServerAsync>d__19 stateMachine = new <ReadCustomerFromServerAsync>d__19();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<NetHandle, byte[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.customer = customer;
        //    stateMachine.send = send;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadFromServerBaseAsync>d__20))]
        //[DebuggerStepThrough]
        //private Task<OperateResult<byte[]>> ReadFromServerBaseAsync(byte[] send)
        //{
        //    <ReadFromServerBaseAsync>d__20 stateMachine = new <ReadFromServerBaseAsync>d__20();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.send = send;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

        //[AsyncStateMachine(typeof(<ReadCustomerFromServerBaseAsync>d__21))]
        //[DebuggerStepThrough]
        //private Task<OperateResult<NetHandle, byte[]>> ReadCustomerFromServerBaseAsync(byte[] send)
        //{
        //    <ReadCustomerFromServerBaseAsync>d__21 stateMachine = new <ReadCustomerFromServerBaseAsync>d__21();
        //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<NetHandle, byte[]>>.Create();
        //    stateMachine.<>4__this = this;
        //    stateMachine.send = send;
        //    stateMachine.<>1__state = -1;
        //    stateMachine.<>t__builder.Start(ref stateMachine);
        //    return stateMachine.<>t__builder.Task;
        //}

	    public override string ToString()
	    {
            return String.Format("NetSimplifyClient[{0}:{1}]", IpAddress, Port);
	    }
    }
}
